import java.util.*;

/**
 * 任务管理系统 - 分支功能：添加任务
 * 功能：
 * 1. 添加任务
 * 2. 更新分类和标签映射
 */
public class TaskAdditionManager {

    // 任务类
    private static class Task {
        private int id;
        private String description;
        private String status;
        private String priority;
        private String category;
        private List<String> tags;

        public Task(int id, String description, String status, String priority, String category) {
            this.id = id;
            this.description = description;
            this.status = status;
            this.priority = priority;
            this.category = category;
            this.tags = new ArrayList<>();
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getPriority() {
            return priority;
        }

        public String getCategory() {
            return category;
        }

        public List<String> getTags() {
            return tags;
        }

        public void addTag(String tag) {
            this.tags.add(tag);
        }

        @Override
        public String toString() {
            return "Task{" +
                    "id=" + id +
                    ", description='" + description + '\'' +
                    ", status='" + status + '\'' +
                    ", priority='" + priority + '\'' +
                    ", category='" + category + '\'' +
                    ", tags=" + tags +
                    '}';
        }
    }

    private static List<Task> tasks = new ArrayList<>();
    private static Map<String, List<Task>> categoryMap = new HashMap<>();
    private static Map<String, List<Task>> tagMap = new HashMap<>();
    private static int nextId = 1;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n===== 任务添加系统 =====");
            System.out.println("1. 添加任务");
            System.out.println("2. 显示所有任务");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addTask(scanner);
                    break;
                case 2:
                    displayAllTasks();
                    break;
                case 0:
                    System.out.println("退出系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    private static void addTask(Scanner scanner) {
        System.out.print("请输入任务描述: ");
        String description = scanner.nextLine();
        System.out.print("请输入任务优先级 (高/中/低): ");
        String priority = scanner.nextLine();
        System.out.print("请输入任务分类: ");
        String category = scanner.nextLine();
        System.out.print("请输入任务标签 (多个标签用逗号分隔): ");
        String tagsInput = scanner.nextLine();

        Task newTask = new Task(nextId++, description, "未完成", priority, category);

        if (!tagsInput.trim().isEmpty()) {
            String[] tagsArray = tagsInput.split(",");
            for (String tag : tagsArray) {
                String trimmedTag = tag.trim();
                if (!trimmedTag.isEmpty()) {
                    newTask.addTag(trimmedTag);
                }
            }
        }

        tasks.add(newTask);

        // 更新分类映射
        categoryMap.computeIfAbsent(category, k -> new ArrayList<>()).add(newTask);

        // 更新标签映射
        for (String tag : newTask.getTags()) {
            tagMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(newTask);
        }

        System.out.println("任务已添加: " + newTask);
    }

    private static void displayAllTasks() {
        if (tasks.isEmpty()) {
            System.out.println("当前没有任务");
        } else {
            System.out.println("\n===== 所有任务 =====");
            for (Task task : tasks) {
                System.out.println(task);
            }
        }
    }
}